﻿$(function () {
    var dialog = $("<div />").dialog({
        title: "登录系统",
        content: $("#login-model").show(),
        width: 400, cache: false, closable: false, height: 250,
        onLoad: function () { dialog.dialog("center"); }
    });
    $(window).resize(function () { dialog.dialog("center"); });

    $("#validate-img").bind("click", function () {
        $.ClearValidate();
    });
});

$.extend({
    //清空验证码
    ClearValidate: function () {
        $("#validate-code").val("");
        $("#validate-img").attr('src', '/Account/GetValidateCode?rd=' + new Date().getTime());
    },

    Clear: function () {
        $("#username").val("");
        $("#password").val("");
        $("#validatecode").val("");
    }
});

//写入等待
function ShowMask() {
    var body = $("body").eq(0);
    var mask = $("<div class=\"datagrid-mask\" style=\"display:block; z-index:77777;\"></div>").appendTo(body);
    var img = $("<img class=\"datagrid-mask-img\" src='/Res/images/loading.gif' style=\"z-index:88888;\" />");
    img.css("left", (body.width() - 214) / 2).css("top", 12).css("position", "absolute").appendTo(body);
}

//移除等待
function HideMask() {
    var body = $("body").eq(0);
    body.children("div.datagrid-mask").remove();
    body.children("img.datagrid-mask-img").remove();
}

//登录
function Login() {
    ShowMask();
    $("#login-model").form("submit", {
        //dataType: "json",
        //contentType: "application/json;charset=UTF-8",
        url: "/Account/Login",
        success: function (result) {
            try {
                result = JSON.parse(result);
                //成功
                if (result.ResultType == 0) {  //ResultType是在OperationResultType定义的，0表示Success成功
                    //alert("成功！");
                    HideMask();
                    //window.location.href = "/Main/Index";
                    window.location = "/Main/Index";
                }
                    //验证码输入错误
                else if (result.ResultType == 2) {
                    HideMask();
                    $("#ViladateCode").val("");
                    $.messager.show({ title: "提示", msg: result.Message });
                }
                else {
                    HideMask();
                    //$.EasyuiMsg(result.Message);  //弹出提示窗体
                    $.messager.show({ title: "提示", msg: result.Message });
                }
            } catch (e) {
                //$.messager.alert({ title: "提示", msg: result });
                HideMask();
                $.messager.alert("提示", result, "info");
            }
        }
    });
}

//清除
function Clear() {
    $("#Account").val("");
    $("#Password").val("");
    $("#ViladateCode").val("");
}
